package com.linngdu664.bettersnowballfight.itemclass;

import com.linngdu664.bettersnowballfight.Register;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.stat.Stats;
import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import net.minecraft.util.Formatting;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;

import javax.annotation.Nullable;
import java.util.List;

public class SnowballStorageTankItem extends Item {
    private final int type;
    public SnowballStorageTankItem(Settings settings, int type) {
        super(settings);
        this.type = type;
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
        ItemStack itemStack = user.getStackInHand(hand);
        if (user.getOffHandStack().isEmpty()) {
            if (user.isSneaking() || itemStack.getDamage() >= 80) {
                switch (type) {
                    case 1 -> user.getInventory().offer(new ItemStack(Register.COMPACTED_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 2 -> user.getInventory().offer(new ItemStack(Register.STONE_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 3 -> user.getInventory().offer(new ItemStack(Register.GLASS_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 4 -> user.getInventory().offer(new ItemStack(Register.IRON_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 5 -> user.getInventory().offer(new ItemStack(Register.GOLD_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 6 -> user.getInventory().offer(new ItemStack(Register.OBSIDIAN_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 7 -> user.getInventory().offer(new ItemStack(Register.EXPLOSIVE_SNOWBALL, 96 - itemStack.getDamage()), true);
                    case 9 -> user.getInventory().offer(new ItemStack(Register.ICE_SNOWBALL, 96 - itemStack.getDamage()), true);
                }
                if (!user.getAbilities().creativeMode)
                    user.setStackInHand(hand, new ItemStack(Register.EMPTY_SNOWBALL_STORAGE_TANK));
            } else {
                switch (type) {
                    case 1 -> user.getInventory().offer(new ItemStack(Register.COMPACTED_SNOWBALL, 16), true);
                    case 2 -> user.getInventory().offer(new ItemStack(Register.STONE_SNOWBALL, 16), true);
                    case 3 -> user.getInventory().offer(new ItemStack(Register.GLASS_SNOWBALL, 16), true);
                    case 4 -> user.getInventory().offer(new ItemStack(Register.IRON_SNOWBALL, 16), true);
                    case 5 -> user.getInventory().offer(new ItemStack(Register.GOLD_SNOWBALL, 16), true);
                    case 6 -> user.getInventory().offer(new ItemStack(Register.OBSIDIAN_SNOWBALL, 16), true);
                    case 7 -> user.getInventory().offer(new ItemStack(Register.EXPLOSIVE_SNOWBALL, 16), true);
                    case 9 -> user.getInventory().offer(new ItemStack(Register.ICE_SNOWBALL, 16), true);
                }
                itemStack.damage(16, user, null);
            }
            user.incrementStat(Stats.USED.getOrCreateStat(this));
        }
        return TypedActionResult.success(itemStack, world.isClient());
    }
    @Override
    public void appendTooltip(ItemStack stack, @Nullable World world, List<Text> tooltip, TooltipContext context) {
        tooltip.add(new TranslatableText("snowball_storage_tank.tooltip").formatted(Formatting.GRAY));
    }
}
